package com.drinkdelivery.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drinkdelivery.common.constants.ProductConstant;
import org.apache.ibatis.javassist.bytecode.LineNumberAttribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.drinkdelivery.mapper.ProductSkuMapper;
import com.drinkdelivery.common.domain.ProductSku;
import com.drinkdelivery.service.IProductSkuService;

/**
 *  SKU Service业务层处理
 *
 * @author lzh
 * @date 2024-11-08
 */
@Service
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku> implements IProductSkuService
{
    @Autowired
    private ProductSkuMapper productSkuMapper;

    /**
     * 查询 SKU
     *
     * @param id  SKU 主键
     * @return  SKU
     */
    @Override
    public ProductSku selectProductSkuById(Long id)
    {
        //根据id获取未逻辑删除的数据
        LambdaQueryWrapper<ProductSku> lqw = new LambdaQueryWrapper<>();
        lqw
                .eq(ProductSku::getId,id)
                .eq(ProductSku::getIsDeleted, 0);
        return this.getOne(lqw);
    }

    /**
     * 查询 SKU 列表
     *
     * @param productSku  SKU
     * @return  SKU
     */
    @Override
    public List<ProductSku> selectProductSkuList(ProductSku productSku)
    {
        return productSkuMapper.selectProductSkuList(productSku);
    }

    /**
     * 新增 SKU
     *
     * @param productSku  SKU
     * @return 结果
     */
    @Override
    public Boolean insertProductSku(ProductSku productSku)
    {
        return this.save(productSku);
    }

    /**
     * 修改 SKU
     *
     * @param productSku  SKU
     * @return 结果
     */
    @Override
    public Boolean updateProductSku(ProductSku productSku)
    {
        return this.updateById(productSku);
    }

    /**
     * 批量删除 SKU
     *
     * @param ids 需要删除的 SKU 主键
     * @return 结果
     */
    @Override
    public boolean deleteProductSkuByIds(Long[] ids)
    {
        LambdaUpdateWrapper<ProductSku> luw = new LambdaUpdateWrapper<>();
        luw
                //未逻辑删除
                .eq(ProductSku::getIsDeleted,0)
                //遍历查询ids
                .in(ProductSku::getId, Arrays.asList(ids))
                //进行逻辑删除
                .set(ProductSku::getIsDeleted, 1);
        return this.update(luw);
    }

    /**
     * 删除 SKU 信息
     *
     * @param id  SKU 主键
     * @return 结果
     */
    @Override
    public Boolean deleteProductSkuById(Long id)
    {
        LambdaUpdateWrapper<ProductSku> luw = new LambdaUpdateWrapper<>();
        luw.eq(ProductSku::getId,id);
        luw.set(ProductSku::getIsDeleted, ProductConstant.DELETE);
        return this.update(luw);
    }


    /**
     * 查询 SKU
     *
     * @param productId  商品id
     * @return  SKU
     */
    @Override
    public List<ProductSku> getProductSkuByProductId(Long productId) {
        LambdaQueryWrapper<ProductSku> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductSku::getProductId,productId);
        lqw.eq(ProductSku::getIsDeleted,ProductConstant.NO_DELETE);

        return this.list(lqw);
    }
}
